// pages/test/test.js
var util = require('../../utils/util.js')
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   * ids 题目id集合
   * page 当前第N道题目
   * article 当前题目详情
   * count 一个count道题目
   * id  当前题目分类id
   * value 当前题目回答
   * action 1下一题 0上一题
   */
  data: {
    ids:'',
    page:0,
    article:'',
    count:0,
    value:'',
    id:'', 
    action:1,
    lenght:0  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (params) {
    this.params = params;
    var params = this.params;  
    console.log(params)
    this.get_question(params.id)  
  },

  radioChange: function (e) {
    console.log('value值为：', e.detail.value);
    var article = this.data.article;
    for (var i = 0, len = article.question_choice.length; i < len; ++i) {
      article.question_choice[i].checked = article.question_choice[i].name == e.detail.value;
    }
    this.setData({
      article: article,
      value: e.detail.value
    });
  },
  next_question(e){
    var page = this.data.page
    var answer_value = this.data.value
    var article_id = this.data.article.id
    var user_info = wx.getStorageSync('user_info');
    var user_id = user_info.user_id;
    var id = this.data.id
    if (answer_value==''){      
      wx.showModal({
        content: '该题您还没回答！！',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }else{
      page = page + 1
      this.setData({
        page: page,
        article_id: article_id,
        id: id,
        action: 1
      })
      this.get_nx_question(answer_value)
     
    }
  },
  prve_question(e) {
    var page = this.data.page
    var answer_value = this.data.value
    var article_id = this.data.article.id
    var user_info = wx.getStorageSync('user_info');
    var user_id = user_info.user_id;
    var id = this.data.id
    if (page == 0) {
      wx.showModal({
        content: '已经到了第一题。',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    } else {
      page = page-1
      this.setData({
        page: page,     
        article_id: article_id,
        id: id,
        action:0
      })
      this.get_nx_question(answer_value)
    
    }
  },
  get_question(id){     
    var that = this;
    var user_info = wx.getStorageSync('user_info');  
    var action = this.data.action
    var d_page = this.data.page + 1
    var count = this.data.count
    var lenght = d_page / count*100;
    if (id && user_info) {
      var ids_url = '/answer/index/answer_question?id=' + id + '&u_id=' + user_info.user_id;
      util.commonAjax(ids_url, 1)
        .then(function (data) {
          var info = data.data.data;
          that.setData({
            ids: info.ids,
            article: info.first_article,           
            count: info.count,
            id: id,
            lenght: lenght,
            value:''
          })
          if (info.first_article.selected){
            that.setData({              
              value: info.first_article.selected
            });
          }
        })
    }
  },
  get_nx_question(answer_value){
    var that = this;
    var page = this.data.page
    var answer_value = answer_value
    var article_id = this.data.article.id
    var id = this.data.id
    var user_info = wx.getStorageSync('user_info');
      var ids_url = '/answer/index/answer_next_question';
      var data = {
        page: page,
        value: answer_value,
        article_id: article_id,
        u_id: user_info.user_id,
        id: id
      }
      util.commonAjax(ids_url, 2, data)
        .then(function (data) {
          if (data.data == true) {           
            that.get_question(id)
          }
      })
    
  }
})